package com.lorenzogao.pley.ui.activity


import android.Manifest
import android.support.v7.app.ActionBarDrawerToggle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.lorenzogao.pley.R
import com.lorenzogao.pley.bean.User
import com.lorenzogao.pley.common.Constant
import com.lorenzogao.pley.common.rx.subscriber.RxBus
import com.lorenzogao.pley.common.util.ACache
import com.lorenzogao.pley.common.util.PermissionUtil
import com.lorenzogao.pley.ui.adapter.ViewPagerAdapter
import com.lorenzogao.pley.ui.base.BaseActivity
import com.lorenzogao.pley.ui.di.component.AppComponent
import com.lorenzogao.pley.ui.presenter.BasePresenter
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar_main.*
import org.jetbrains.anko.onClick
import org.jetbrains.anko.toast


class MainActivity : BaseActivity<BasePresenter<*, *>>() {


    private var glideRequest: RequestManager? = null

    override fun initView() {
        RxBus.getDefault().toObservable(User::class.java).subscribe(Consumer<User> { user -> initUserHeadView(user) })
    }


    override fun setupActivityComponent(appComponent: AppComponent) {

    }


    override fun setLayout(): Int {

        return R.layout.activity_main
    }


    override fun initData() {

        PermissionUtil.requestPermission(this, Manifest.permission.READ_PHONE_STATE).subscribe(Consumer<Boolean> { aBoolean ->
            if (aBoolean) {
                initDrawerLayout()

                initTabLayout()

                initUser()

            }
        })


    }


    private fun initTabLayout() {
        var mAdapter = ViewPagerAdapter(supportFragmentManager)
        view_pager.adapter = mAdapter
        view_pager.offscreenPageLimit = mAdapter.count
        tab_layout.setupWithViewPager(view_pager)


    }


    private var headView: View? = null

    private fun initDrawerLayout() {

        headView = nav_view.getHeaderView(0)
        nav_view.setNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.menu_app_update -> JumAct(AppManagerActivity::class.java)
                R.id.menu_message -> toast("消息中心")
                R.id.menu_setting -> toast("设置")
                R.id.menu_outlogin -> logout()
            }
            false
        }






        setSupportActionBar(tool_bar)
        var drawerTooggle = ActionBarDrawerToggle(this, drawer_layout, tool_bar, R.string.open, R.string.close)
        drawerTooggle.syncState()
        drawer_layout.addDrawerListener(drawerTooggle)

    }


    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.toolbar_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        when (item.itemId) {
            R.id.action_down -> JumAct(AppManagerActivity::class.java)


        }



        return true

    }


    private fun initUserHeadView(user: User) {
        var img = headView!!.findViewById<ImageView>(R.id.iv_user_icon)
        var tv_user = headView!!.findViewById<TextView>(R.id.tv_user_name)
        Glide.with(this).load("https:" + user.logo_url).apply(RequestOptions.circleCropTransform()).into(img)
        tv_user.text = user.username

    }

    private fun initUser() {
        var user = ACache.get(this).getAsObject(Constant.USER)
        if (user == null) {
            headView!!.onClick {
                JumAct(LoginActivity::class.java)
            }
        } else {
            var users: User = user as User
            initUserHeadView(users)
        }
    }


    private fun logout() {
        var mAChech = ACache.get(this)
        mAChech.put(Constant.TOKEN, "")
        mAChech.put(Constant.USER, "")

        var img = headView!!.findViewById<ImageView>(R.id.iv_user_icon)
        var tv_user = headView!!.findViewById<TextView>(R.id.tv_user_name)
        img.setImageResource(R.mipmap.ic_launcher)
        tv_user.text = "未登录"
        toast("已退出登陆")
    }

    override fun onDestroy() {
        super.onDestroy()
//        RxBus.get().unregister(this)
    }


}

